<template>
  <div class="newProject">
    <div style="width: 95%;height: 838px; margin: auto;margin-top: 20px;">
      <div style="width: 100%;height: 46px;align-items: center;border-bottom: 2px solid #DCDFE6;margin-bottom: 30px; display: flex;
  align-items: center;  ">
        <span
          style="font-family: PingFangSC-Medium;font-weight: 500;font-size: 16px;color: #303133;line-height: 24px;">新增外委</span>
      </div>
      <div>
        <el-form ref="form" :model="form" :rules="rules">
          <el-row type="flex" justify="space-between"
            style="border: 1px solid #DCDFE6;height: 56px;background-color: #F5F7FA;">
            <el-col :span="5" class="colLeft">
              <span class="required">项目名称：</span>
            </el-col>
            <el-col :span="6" class="colRight">
              <el-form-item label="" prop="projectId">
                <el-select v-model="form.projectId" filterable placeholder="请选择项目" style="width: 100%;"
                  @change="projectChange">
                  <el-option v-for=" item in projectOptions" :key="item.id" :label="item.name" :value="item.id">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="5" class="colLeft">
              <span class="required">任务名称</span>
            </el-col>
            <el-col :span="6" class="colRight">
              <el-form-item label="" prop="taskId">
                <el-select v-model="form.taskId" filterable placeholder="请选择任务" style="width: 100%;">
                  <el-option v-for=" item in taskOptions" :key="item.id" :label="item.name" :value="item.id">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row type="flex" justify="space-between"
            style="border: 1px solid #DCDFE6;height: 56px;background-color: #F5F7FA;">
            <el-col :span="5" class="colLeft">
              <span class="required">项目总金额(万元)</span>
            </el-col>
            <el-col :span="18" class="colRight">
              <el-form-item label="" prop="projectFee">
                <el-input v-model="form.projectFee" @change="calculateFee" placeholder="请输入总金额"
                  style="height: 28px !important;"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row type="flex" justify="space-between"
            style="border: 1px solid #DCDFE6;height: 140px;background-color: #F5F7FA;">
            <el-col :span="5" class="colLeft">
              <span class="required">项目工作内容与任务</span>
            </el-col>
            <el-col :span="18" class="colRight">
              <el-form-item label="" prop="projectContent">
                <el-input type="textarea" :rows="5" placeholder="请输入项目工作内容与任务" v-model="form.projectContent">
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row type="flex" justify="space-between"
            style="border: 1px solid #DCDFE6;height: 140px;background-color: #F5F7FA;">
            <el-col :span="5" class="colLeft">
              <span class="required">计划外委工作内容与任务量</span>
            </el-col>
            <el-col :span="18" class="colRight">
              <el-form-item label="" prop="content">
                <el-input type="textarea" :rows="5" placeholder="请输入计划外委工作内容与任务量" v-model="form.content">
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row type="flex" justify="space-between"
            style="border: 1px solid #DCDFE6;height: 56px;background-color: #F5F7FA;">
            <el-col :span="5" class="colLeft">
              <span class="required">计划外委金额（万元）：</span>
            </el-col>
            <el-col :span="6" class="colRight">
              <el-form-item label="" prop="amount">
                <el-input v-model="form.amount" @change="calculateFee" placeholder="请输入计划外委金额"
                  style="height: 28px !important;"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="5" class="colLeft">
              <span class="required">计划外委金额占项目总金额比例（%）：</span>
            </el-col>
            <el-col :span="6" class="colRight">
              <el-form-item label="" prop="proportion">
                <el-input v-model="form.proportion" placeholder="请输入计划外委金额占项目总金额比例（%）"
                  style="height: 28px !important;"></el-input>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row type="flex" justify="space-between"
            style="border: 1px solid #DCDFE6;height: 56px;background-color: #F5F7FA;">
            <el-col :span="5" class="colLeft">
              <span class="required">外委供应商名称</span>
            </el-col>
            <el-col :span="6" class="colRight">
              <el-form-item label="" prop="email">
                <el-select v-model="form.enterpriseId" filterable placeholder="请选择项目" style="width: 100%;"
                  @change="projectChange">
                  <el-option v-for=" item in enterpriseOptions" :key="item.id" :label="item.name" :value="item.id">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="5" class="colLeft">
              <span class="required">人数：</span>
            </el-col>
            <el-col :span="6" class="colRight">
              <el-form-item label="" prop="number">
                <el-input v-model="form.number" placeholder="请输入人数" style="height: 28px !important;"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row type="flex" justify="space-between"
            style="border: 1px solid #DCDFE6;height: 100px;background-color: #F5F7FA;">
            <el-col :span="5" class="colLeft">
              <span class="required">外委原因：</span>
            </el-col>
            <el-col :span="18" class="colRight">
              <el-form-item label="" prop="reason">
                <el-input type="textarea" :rows="3" placeholder="请输入外委原因" v-model="form.reason">
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item style="margin-top: 30px;">
            <el-button type="primary" @click="onSubmit" style="background-color: #47ACF4;">确定</el-button>
            <el-button type="primary" @click="cancel"
              style="margin-left: 30px;background: transparent; color: #47ACF4;margin-right:24px ;">取消</el-button>
            <!-- <el-button>取消</el-button> -->
          </el-form-item>
        </el-form>
      </div>

    </div>
  </div>
</template>

<script>
import { addOutsourcing } from '@/api/outsourcingManagement/outsourcing'
import { getProjectList } from "@/api/project/project";
import { getSupplierList } from "@/api/outsourcingManagement/supplier";
import { getTaskList } from '@/api/project/task'
import { downloadGet } from '@/utils/request'
export default {
  components: {
  },
  data() {
    var checkPhone = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('手机号不能为空'));
      } else {
        const reg = /^1[3|4|5|7|8][0-9]\d{8}$/
        console.log(reg.test(value));
        if (reg.test(value)) {
          callback();
        } else {
          return callback(new Error('请输入正确的手机号'));
        }
      }
    };
    return {
      canRadio: false,
      hasProject: true,
      fileList: [],
      form: {},
      projectOptions: [],
      taskOptions: [],
      natureOptions: [],
      deptOptions: [],
      enterpriseOptions: [],
      businessScopeListOptions: [],
      inputDisabled: true,
      rules: {
        name: [
          { required: true, message: '请输入供应商名称', trigger: 'blur' },
        ],
        bank: [
          { required: true, message: '请输入开户行', trigger: 'blur' },
        ],
        credential: [
          { required: true, message: '输入资质', trigger: 'blur' },
        ],
        account: [
          { required: true, message: '请输入账号', trigger: ["blur", "change"] },
          { type: 'number', message: '请输入数字类型', trigger: 'blur' },
        ],
        contactor: [
          { required: true, message: '请输入联系人', trigger: 'blur' },
        ],
        telephone: [
          { validator: checkPhone, trigger: 'blur' }
        ],
        email: [{ type: "email", message: "请输入正确的邮箱地址", trigger: ["blur", "change"] }
        ],
        businessScope: [
          { required: true, message: '请选择经营范围', trigger: 'blur' },
        ],
        address: [
          { required: true, message: '请输入注册地址', trigger: 'blur' },
        ],
        members: [
          { required: true, message: '请输入成员及职务', trigger: 'blur' },
        ],
        achievements: [
          { required: true, message: '请输入主要业绩', trigger: 'blur' },
        ],
      },
      taskUnitOptions: [],
      projectNo: '',
      taskNo: 1
    }
  },
  watch: {
  },
  mounted() {
    getProjectList().then(res => {
      this.projectOptions = res.rows
    });
    getSupplierList().then(res => {
      this.enterpriseOptions = res.rows
    });
  },
  methods: {
    projectChange(id) {
      getTaskList({ projectId: id }).then(res => {
        let obj = this.projectOptions.find(obj => obj.id === id);
        this.form.projectFee = obj.totalFee
        console.log(this.form.projectFee)
        this.taskOptions = res.rows
        console.log(taskOptions)
      })
    },
    async onSubmit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          addOutsourcing(this.form).then(res => {
            if (res.code == 200) {
              this.$modal.msgSuccess('新增供应商成功！')
              this.$router.push({
                path: '/outsourcingOverview',
              });
            }
          })
        } else {
          this.$modal.msgError('请填写完完整信息！');
        }
      })

    },
    calculateFee() {
      if (this.form?.amount != null && this.form?.projectFee != null) {
        this.form.proportion = (this.form.amount / this.form.projectFee) * 100
        console.log('proportion', this.form.proportion)
      }
      console.log('amount', this.form.amount)
    },
    cancel() {
      this.$router.go(-1);
    },
    getDict(dictName) {
      let dict = this.$store.state.dictNew[dictName]
      console.log(dict)
      return dict
    },
    //附件移除
    handleRemove(file, fileList) {
      this.fileList = fileList;
    },
    // 材料改变
    handleChange(file, fileList) {
      const res = this.beforeAvatarUpload(file, fileList);
      if (!res || fileList.length >= 2) {
        fileList.shift();
      }
      this.fileList = fileList;
    },
    // 上传前控制
    beforeAvatarUpload(file, fileList) {
      const isLt2M = file.size / 1024 / 1024 < 100;
      if (!isLt2M) {
        this.$message.error(`上传图片大小不能超过100MB!`);
      }
      return isLt2M;
    },
    // 下载附件
    downLoad(file) {
      return downloadGet(this.downBaseUrl + file.path, {}, file.name);
    },
  }
}
</script>

<style scoped lang="scss">
.newProject {
  width: 95%;
  height: 100%;
  background-color: #FFFFFF;
  margin-top: 20px;

  .colRight {

    padding: 10px;
    background-color: #FFFFFF;
  }

  .colLeft {
    /* border: 1px solid #DCDFE6; */
    background-color: #F5F7FA;
    text-align: center;
    margin: auto;
  }
}

span.required::after {
  content: "*";
  color: #f56c6c;
  margin-right: 4px;
}
</style>

<style>
.el-form-item__error {
  z-index: 999 !important;
}
</style>
